package com.primb.bench.project.init.presenter;


import android.graphics.Color;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;


import com.primb.bench.project.init.contract.MainContract;
import com.primb.bench.project.init.model.MainModelImpl;


/**
 * Created by Administrator on 2017/04/14
 */

public class MainPresenterImpl implements MainContract.Presenter {
    private MainContract.View mView;
    private MainContract.Model mModel;

    public MainPresenterImpl(MainContract.View view) {
        this.mView = view;
        mModel = new MainModelImpl();
    }


    @Override
    public void start() {
        setDefaultFragment();
    }

    @Override
    public void onRecycle() {

    }

    @Override
    public void showFragment(int position) {
        FragmentTransaction transaction = mView.getActivityFragmentManager().beginTransaction();
        hideFragments(transaction);
        mView.clearState(mModel.getNormalIcons());
        mView.selectState(position, mModel.getSelectIcons());
        android.support.v4.app.Fragment fragment = mView.getFragmentList().get(position);
        if (!fragment.isAdded()) {
            transaction.add(mView.getLayoutId(), fragment).show(fragment);
        } else {
            transaction.show(fragment);
        }
        transaction.commit();
    }

    @Override
    public void hideFragments(FragmentTransaction transaction) {
        for (int i = 0; i < mView.getFragmentList().size(); i++) {
            if (mView.getFragmentList().get(i) != null) {
                transaction.hide(mView.getFragmentList().get(i));
            }
        }
    }

    @Override
    public void setDefaultFragment() {
        showFragment(0);
    }
}